<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4、变位词组</title>
</head>

<body>
  <script>
    var groupAnagrams = function (strs) {
      let map = new Map()
      for (let str of strs) {
        // 字符串排序
        let key = str.split('').sort().join('')
        console.log(key)
        // set:如果key已经有值，则键值会被更新，否则就新生成该键
        // get:读取key对应的键值，如果找不到key，返回undefined
        //has:返回一个布尔值，表示某个键是否在当前 Map 对象之中
        map.has(key) ? map.get(key).push(str) : map.set(key, [str])
      }
      return [...map.values()]
    };
    console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
  </script>
</body>

</html>